package services
{
	import events.SwitchPageEvent;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import model.Magazine;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.events.FlexEvent;

	public class DisplayedMagazineManager extends EventDispatcher
	{
		
		private var _currentMagazine:Magazine;
		private var _currentPage:int;
		private var _pagesCollection:ArrayCollection;

		public function DisplayedMagazineManager()
		{
		}
		
		public function set currentPage(p:int):void
		{
			_currentPage = p;
			dispatchEvent(new SwitchPageEvent(SwitchPageEvent.CHANGE_PAGE, p, true));
		}
		public function get currentPage():int
		{
			return _currentPage;
		}
		
		public function set currentMagazine(magazine:Magazine):void
		{
			if(_currentMagazine != magazine)
			{
				_currentMagazine = magazine;
				_pagesCollection = magazine.pages;
				currentPage = 0;
			//	loadCoverIntoView();
			}
		}
		public function get currentMagazine():Magazine
		{
			return _currentMagazine;

		}
		
		[Bindable(event="ChangePage")]
		public function getLeftPage():String
		{
			var result:String = null;
			if(currentPage==0)
				// cover page - it should be on the right side, so no image here
				result = "images/emptypage.jpg";
			else if(currentPage%2 == 1)
			{
				result = String(_pagesCollection.getItemAt(currentPage));
			}
			else
			{
				result = String(_pagesCollection.getItemAt(currentPage-1));
			}
			return result;
		}
		
		[Bindable(event="ChangePage")]
		public function getRightPage():String
		{
			if(currentPage%2 == 0)
				return String(_pagesCollection.getItemAt(currentPage));
			else if(currentPage < _pagesCollection.length - 1)
				return String(_pagesCollection.getItemAt(currentPage+1));
			else
				// last page is an odd number, right page is empty
				return "images/emptypage.jpg";
		}
		
		private function loadCoverIntoView():void
		{
			getLeftPage();
			getRightPage();
		}
		
		
		protected function addEventListeners(event:FlexEvent):void
		{
			addEventListener(SwitchPageEvent.CHANGE_PAGE, loadPage, true);
		}
		
		protected function loadPage(event:SwitchPageEvent):void
		{
			currentPage = event.page;
		}
		
		public function getNextPage():int
		{
			var nextPage:int;
			if(currentPage%2 == 0 && currentPage < _pagesCollection.length-1)
				nextPage = currentPage + 1;
			else if(currentPage%2 == 1 && currentPage<_pagesCollection.length - 2)
				nextPage = currentPage + 2;
			else
				nextPage = currentPage;
			return nextPage;
		}
		
		public function getPreviousPage():int
		{
			var previousPage:int = currentPage;
			if(currentPage%2==0 && currentPage>1)
				previousPage = currentPage - 2;
			else if(currentPage%2==1)
				previousPage = currentPage - 1;
			return previousPage;
		}
		
		public function isFirstPage():Boolean
		{
			if(currentPage==0) return true;
			else return false;
		}
		
		public function isLastPage():Boolean
		{
			if(_pagesCollection.length % 2 == 0)
				{
					if(currentPage >= _pagesCollection.length - 1) return true;
					else return false;
				}
				else
				{
					if(currentPage >= _pagesCollection.length - 2) return true;
					else return false;
				}				
		}
		
		[Bindable(event="ChangePage")]
		public function displayedPagesToString():String
		{
			var result:String = "";
			if(currentPage == 0)
				result = "Cover";
			else if(currentPage%2 == 0)
			{
				result += (currentPage - 1);
				result += "-";
				result += currentPage;
			}
			else
			{
				result += currentPage;
				if(currentPage < _pagesCollection.length - 1)
				{
					result += "-";
					result += (currentPage+1);
				}
			}
			
			return result;
		}
	}
}